package org.apache.ibatis.common.reflection.property;

import java.util.Iterator;

/**
 * @Name PropertyTokenizer
 * @Description: 属性标记器
 * 描述信息：该类主要实现嵌套属性字符串解析（迭代 & 拷贝）操作。
 * 注意：该属性标记其只能处理一级嵌套属性，即点后面的全部作为一级子节点。
 * 例如，student[stuId].name -> 解析为 stuId = 1, 2, 3, ... 的学生的 name。
 * @Version v1.0.0
 * @Author: lichao
 * @Date: 2024-04-25 15:56:47
 **/
public class PropertyTokenizer implements Iterator<PropertyTokenizer> {
    private String name;
    private final String indexedName;
    private String index;
    private final String children;

    public PropertyTokenizer(String fullname) {
        int delim = fullname.indexOf(".");
        if (delim > -1) {
            name = fullname.substring(0, delim);
            children = fullname.substring(delim + 1);
        } else {
            name = fullname;
            children = null;
        }
        indexedName = name;
        delim = name.indexOf("[");
        if (delim > -1) {
            name = name.substring(0, delim);
            index = name.substring(delim + 1);
        }
    }

    @Override
    public boolean hasNext() {
        return children != null;
    }

    @Override
    public PropertyTokenizer next() {
        return new PropertyTokenizer(children);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("该属性标记器不支持 Remove 方法，因为它在属性文件上下文中没有意义。");
    }

    public String getName() {
        return name;
    }

    public String getIndexedName() {
        return indexedName;
    }

    public String getIndex() {
        return index;
    }

    public String getChildren() {
        return children;
    }
}

